//商家入驻 功能js 2017-8-23 09:49:02
var appUrl = $("#_ctx").val();
/*搜索品牌*/
function searchBrand(){
    layer.open({
        type: 2,
        area: ['700px', '600px'],
        offset: [100,(($(window).width()-900)/2)],
        title: '添加品牌',
        content: [appUrl+'/merchant/search','no']
    });
}
/*删除品牌*/
function removeBrand(obj,id){
    var str = $('#brandId').val();
    var strArray = str.split(",");
    var selInput = $(obj).parent().attr('id');
    var index = strArray.indexOf(selInput);
    if (index > -1) {strArray.splice(index, 1);}
    str = strArray.join(',');
    $('#brandId').val(str).change();
    $(obj).parent().remove();
    $.ajax({
        type: 'GET',
        url: appUrl+'admin/shop/delShopAndBrand?id=' + id,
        dataType: 'json',
        success: function (data) {
            if (data.code == '0') {
            }
        }
    });
}
//填充品牌
function fillBrand(){
    if($('#brandId').val()!=''){
        var arrBrand = $('#brandId').val();
        var arrBrandArray = arrBrand.split(",");
        for(var i=0;i<arrBrandArray.length;i++){
            var id =arrBrandArray[i];
            $.ajax({
                type:"get",
                url:appUrl+'/admin/brand/searchById?id=' + id,
                dataType:'json',
                success:function (data){
                    var str = '<span id="'+data.id+'" class="brand-littlebox">'+data.name+' <i class="fa fa-times text-danger" title="移除" onclick="removeBrand(this)"></i></span>';
                    $('#brandDivParent').show();
                    $('#brandDiv').append(str);
                }
            });
        }
    }
}

//省
function initTrip(proval){
    $.ajax({
        type:"get",
        url:appUrl+'/trip/area/proList',
        dataType:'json',
        success:function (data){
            var prohtml="<option value=''>请选择省份</option>";
            data = data.result;
            for (var i=0;i<data.length;i++) {
                if (proval==data[i].proCode) {
                    prohtml=prohtml+("<option selected='selected' value="+data[i].proCode+">"+data[i].proName+"<\/option>");
                }else{
                    prohtml=prohtml+("<option  value="+data[i].proCode+">"+data[i].proName+"<\/option>");
                }
            }
            $("#tripPro").html(prohtml);
            $("#tripCity").html("<option value=''>请选择城市</option>");
            $("#tripDis").html("<option value=''>请选择区/县</option>");
        }
    }).done(function(){
        if(proval != ""){
            tripProvince(proval);
        }
        $("#tripPro").change(function(){
            var id = $(this).val();
            tripProvince(id);
        });
    });
}

//市
function tripProvince(id){
    var cityVal = "";
    $.ajax({
        type:"get",
        url:appUrl+'/trip/area/cityList/'+id,
        dataType:'json',
        success:function (data){
            var cityhtml="<option value=''>请选择城市</option>";
            data = data.result;
            for (var i=0;i<data.length;i++) {
                if (cityVal==data[i].cityCode) {
                    cityhtml=cityhtml+("<option selected='selected' value="+data[i].cityCode+">"+data[i].cityName+"<\/option>");
                }else{
                    cityhtml=cityhtml+("<option  value="+data[i].cityCode+">"+data[i].cityName+"<\/option>");
                }
            }
            $("#tripCity").html(cityhtml);
            $("#tripDis").html("<option value=''>请选择区/县</option>");
        }
    }).done(function(){
        if(cityVal != ""){
            var id = $("#tripCity").val();
            tripCity(id);
        }
        $("#tripCity").change(function(){
            var id = $(this).val();
            tripCity(id);
        });
    });
}

//区/县
function tripCity(id){
    $.ajax({
        type:"get",
        url:appUrl+'/trip/area/disList/'+id,
        data:{id:id},
        dataType:'json',
        success:function (data){
            var areaVal = "";
            var areahtml="<option value=''>请选择区/县</option>";
            data = data.result;
            for (var i=0;i<data.length;i++) {
                if(areaVal==data[i].disCode){
                    areahtml=areahtml+("<option selected='selected' value="+data[i].disCode+">"+data[i].disName+"<\/option>");
                }else{
                    areahtml=areahtml+("<option value="+data[i].disCode+">"+data[i].disName+"<\/option>");
                }
            }
            $("#tripDis").html(areahtml);
        }
    });
}

//获取验证码
var codeStatus = true;
function getCode(tel){
    if(codeStatus){
        codeStatus = false;
        $.ajax({
            type: 'GET',
            url: appUrl+'/merchant/sendCode?account='+tel+'&imageCodeType=41',
            dataType: 'json',
            cache: false,
            success: function(data) {
                if(data.code == "0"){
                    Count($(".code-btn"));
                }
                else if(data.code == "00001026"){
                    alert("该手机号已占用!")
                }
            },
            error: function(){
                alert("获取验证码失败")
            }
        })
    }
}
//倒计时
var Count = function(obj){
    var time = 60;//时间
    //定义时间
    var t = setInterval(function(){
        time --;
        obj.html(time + "s后重试");
        if(time > 0){
            obj.off("click");
            obj.css("background-color","#ccc");
        }
        else if(time == 0){
            clearInterval(t);
            obj.css("background-color","#FFBE34");
            obj.html("重新获取");
            codeStatus = true;
            obj.off("click").on("click",function(){
                var tel = $("#tel").val();
                getCode(tel);
            });
        }
    },1000);
};

//新增上传框
$('.add-inputfile1').on('click',function(){
    addImg('instrImg',this);
});
$('.add-inputfile2').on('click',function(){
    addImg('natureImg',this);
});
$('.add-inputfile3').on('click',function(){
    addImg('goodImg',this);
});
$('.add-inputfile4').on('click',function(){
    addImg('serviceImg',this);
});
function addImg(name1,obj){
    var str1 = "<div class='inputfile-img-box'><input class='obj-input' name="+name1+" value='' type='hidden'>" +
        "<img class='obj-img' src='' style='display:none' width='100' height='100'><span class='input-file'>" +
        "<input data-image="+name1+" data-action='goods' type='file' onchange='uploadImg(this)'></span><i class='fa fa-close'></i></div>";
    var len = $(obj).parent().find(".inputfile-img-box").length;
    if(len < 4){
        $(obj).before(str1);
    }
    else{
        return false
    }
    $("i.fa-close").click(function(){
        $(this).parent().remove();
    });
}